package com.yz.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.yz.common.annotation.Login;
import com.yz.common.annotation.LoginUser;
import com.yz.common.entity.*;
import com.yz.common.entity.SurveyRecordEntity;
import com.yz.common.utils.R;
import com.yz.common.validator.ValidatorUtils;
import com.yz.service.CheckService;
import com.yz.service.DiscoverRecordService;
import com.yz.service.SurveyRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.HashMap;
import java.util.Map;

/**
 *记录相关操作
 *
 * @author yz

 *
 */
@RestController
@RequestMapping("/api")
@Api(tags="记录")
public class ApiRecordController {
    @Autowired
    private SurveyRecordService surveyRecordService;
    @Autowired
    private DiscoverRecordService discoverRecordService;
    @Autowired
    private CheckService checkService;
    @GetMapping("getDiscoverRecordDetail")
    @ApiOperation("查看-挖掘记录-详情")
    @Login
    public R getDiscoverRecordDetail(@RequestHeader("token")String token,@RequestParam("id") long id){
        try {
            DiscoverRecordForm data = discoverRecordService.getSpecimenDetail(id);
            return R.ok().put("data",data);
        }catch (Exception e){
            return R.error().put("data",null);
        }

    }
    @DeleteMapping("deleteDiscoverRecord")
    @ApiOperation("删除-挖掘记录")
    @Login
    public R deleteDiscoverRecord(@RequestHeader("token")String token,@RequestParam("id") long id){
        try {
            discoverRecordService.deleteSpecimen(id);
            return R.ok().put("data",null);
        }catch (Exception e){
            return R.error().put("data",null);
        }
    }
    @PostMapping("postDiscoverRecord")
    @ApiOperation("提交或修改-挖掘记录")
    @Login
    public R postDiscoverRecord(@RequestHeader("token")String token, @RequestBody DiscoverRecordForm t, @ApiIgnore @LoginUser UserEntity user){
        ValidatorUtils.validateEntity(t);
        try {
            discoverRecordService.postSpecimen(t);
            return R.ok().put("data",null);
        }catch (Exception e){
            return R.error().put("data",null);
        }
    }

    @GetMapping("getDiscoverRecordList")
    @ApiOperation("获取-挖掘记录-列表")
    @Login
    public R getDiscoverRecordList(@RequestHeader("token")String token,@RequestParam("order")int order,@RequestParam(value = "keyWord",required = false) String keyWord,@RequestParam("page") int page,@RequestParam("pageSize") int pageSize,@RequestParam("projectId") long projectId, @ApiIgnore @LoginUser UserEntity user){
        int total=0;
        Page pages =null;
        String group = "create_at desc,id desc";
        if(order!=0){
            group = "create_at asc,id asc";
        }
        if(keyWord==null||keyWord.isEmpty()){
            total = checkService.selectCount(new EntityWrapper<CheckEntity>().eq("project_id",projectId).eq("type_form",2).eq("type",6).eq("create_user",user.getUsername()));
            pages = checkService.selectPage(new Page<CheckEntity>(page+1,pageSize),new EntityWrapper<CheckEntity>().eq("type_form",2).eq("type",6).eq("project_id",projectId).eq("create_user",user.getUsername()).groupBy(group));
        }else{
            total = checkService.selectCount(new EntityWrapper<CheckEntity>().eq("project_id",projectId).eq("type_form",2).eq("type",6).eq("create_user",user.getUsername()).where("(number like '%"+keyWord+"%' or name_form like '%"+keyWord+"%')"));
            pages = checkService.selectPage(new Page<CheckEntity>(page+1,pageSize),new EntityWrapper<CheckEntity>().eq("type_form",2).eq("type",6).eq("project_id",projectId).eq("create_user",user.getUsername()).where("(number like '%"+keyWord+"%' or name_form like '%"+keyWord+"%')").groupBy(group));
        }
        return R.ok().put("data",pages.getRecords()).put("total",total);
    }
    @GetMapping("getSurveyRecordDetail")
    @ApiOperation("查看-勘探记录-详情")
    @Login
    public R getSurveyRecordDetail(@RequestHeader("token")String token,@RequestParam("id") long id){
        SurveyRecordEntity t = surveyRecordService.selectOne(new EntityWrapper<SurveyRecordEntity>().eq("id",id));
        return R.ok().put("data",t);
    }
    @DeleteMapping("deleteSurveyRecord")
    @ApiOperation("删除-勘探记录")
    @Login
    public R deleteSurveyRecord(@RequestHeader("token")String token,@RequestParam("id") long id){
        try {
            surveyRecordService.deleteMe(id);
            return R.ok().put("data",null);
        }catch (Exception e){
            return R.error().put("data",null);
        }
    }
    @PostMapping("postSurveyRecord")
    @ApiOperation("提交或修改-勘探记录")
    @Login
    public R postSurveyRecord(@RequestHeader("token")String token, @RequestBody SurveyRecordEntity t, @ApiIgnore @LoginUser UserEntity user){
        ValidatorUtils.validateEntity(t);
        try {
            surveyRecordService.postMe(t);
            return R.ok().put("data",null);
        }catch (Exception e){
            return R.error().put("data",null);
        }
    }

    @GetMapping("getSurveyRecordList")
    @ApiOperation("获取-勘探记录-列表")
    @Login
    public R getSurveyRecordList(@RequestHeader("token")String token,@RequestParam("order")int order,@RequestParam(value = "keyWord",required = false) String keyWord,@RequestParam("page") int page,@RequestParam("pageSize") int pageSize,@RequestParam("projectId") long projectId, @ApiIgnore @LoginUser UserEntity user){
        int total=0;
        Page pages=null;
        String group = "create_at desc,id desc";
        if(order!=0){
            group = "create_at asc,id asc";
        }
        if(keyWord==null||keyWord.isEmpty()){
            total = checkService.selectCount(new EntityWrapper<CheckEntity>().eq("project_id",projectId).eq("type_form",1).eq("type",6).eq("create_user",user.getUsername()));
            pages = checkService.selectPage(new Page<CheckEntity>(page+1,pageSize),new EntityWrapper<CheckEntity>().eq("type_form",1).eq("type",6).eq("project_id",projectId).eq("create_user",user.getUsername()).groupBy(group));
        }else {
            total = checkService.selectCount(new EntityWrapper<CheckEntity>().eq("project_id",projectId).eq("type_form",1).eq("type",6).eq("create_user",user.getUsername()).where("(number like '%"+keyWord+"%' or name_form like '%"+keyWord+"%')"));
            pages = checkService.selectPage(new Page<CheckEntity>(page+1,pageSize),new EntityWrapper<CheckEntity>().eq("type_form",1).eq("type",6).eq("project_id",projectId).eq("create_user",user.getUsername()).where("(number like '%"+keyWord+"%' or name_form like '%"+keyWord+"%')").groupBy(group));
        }
        return R.ok().put("data",pages.getRecords()).put("total",total);
    }
}
